'use strict';

const Service = require('egg').Service;

class CategoryService extends Service {

  // 新增商品分类
  async create(payload) {
    const { app } = this;
    return await app.model.MallCategory.create(payload);
  }

  // 获取商品分类列表
  async getList(payload) {
    const { app } = this;
    const { pageNum, pageSize } = payload;
    const { count, rows } = await app.model.MallCategory.findAndCountAll({
      offset: (pageNum - 1) * pageSize,
      limit: pageSize * 1,
    });
    return {
      pageNum,
      pageSize,
      total: count,
      list: rows,
    };
  }

  // // 删除商品分类
  async destroy(payload) {
    const { app } = this;
    return await app.model.MallCategory.destroy({ where: { id: payload.ids }, force: true });
  }

  // 修改商品分类
  async update(id, payload) {
    const { app } = this;
    return await app.model.MallCategory.update(payload, { where: { id } });
  }

}

module.exports = CategoryService;
